#!/bin/perl

# process Ottawa text file into format I can paste into Excel:
# team name <tab> time


my $fileName;
if ($#ARGV >= 0)
{
   $fileName = $ARGV[0]; 
}
else
{
   $fileName = "D:\\dragon boat\\2009\\Regression\\OttawaSat.txt";
}


my $outFile;
my $i = 0;

my $teamName = 0;
my $category = 0;
my $time = 0;
my $raceNo = 0;
my $distance = 500;
my $festival = "Ottawa";
my $year = 2009;

print("Starting parser\n");

# Read file into memory
# -----------------------------------------------------------------------------
open(F, "<$fileName") || die("Could not open $fileName");
my @lines=<F>;
my $line;
close(F);


$outFile = "$fileName.out";
open(F, ">$outFile") || die("Could not open $outFile");

 
for ($i=1;$i<=$#lines; $i++)
{
   $line=$lines[$i];
   chomp($line);


   if ($line =~ m/Race No\.: (\d+)/ )
   {
      $raceNo = $1;
      $distance = 500;
      $category = "Mixed";
   }


   if ($line =~ m/250m/  ||
       $line =~ m/250 m final/ )
   {
      print("matched 250m\n");
      $distance = 250;
   }
   elsif ($line =~ m/200m/)
   {
      print("matched 200m\n");
      $distance = 200;
   }
   elsif ($line =~ m/100m/)
   {
      print("matched 100m\n");
      $distance = 100;
   }


   # Update current category
   if ($line =~ m/Women's Division/ || 
       $line =~ m/Women's \d{3}m/ ||
       $line =~ m/Women's \d{3} m/)
   {  
      print("matched women\n");
      $category = "Women";
   }

   # Update current category
   if ($line =~ m/Open Division/ || 
       $line =~ m/Open's \d{3}m/ ||
       $line =~ m/Open's \d{3} m/)
   {  
      print("matched open\n");
      $category = "Open";
   }

   # Update current category
   if ($line =~ m/Breast Cancer Survivors/ )
   {  
      $category = "Women";
   }

     
   if ($line =~ m/\d (.*)\s+(\d:\d\d\.\d\d)/ )
   {
      #print("1=$1          2=$2 \n");
      $teamName = $1;
      $time = $2;
      if ($teamName =~ m/\(W\)/)
      {
         $category = "Women";
      }
      else
      {
         $category = "Mixed";
      }
      

      $teamName =~ s/\(W\)//;
      $teamName =~ s/Provincial & Federal Government//;
      $teamName =~ s/High Tech//;
      $teamName =~ s/Girls n' Women and Sport \(women's//;
      $teamName =~ s/Education//;
      $teamName =~ s/Professional Groups//;
      $teamName =~ s/Health Care & Ambulance Services//;
      $teamName =~ s/Financial Services//;
      $teamName =~ s/Building & Development//;
      $teamName =~ s/Hospitality & Tourism//;
      $teamName =~ s/Competitive//;
      $teamName =~ s/Media//;
      $teamName =~ s/Police & Firefighters//;
      $teamName =~ s/Legal//;
      $teamName =~ s/Over 40//;
      $teamName =~ s/Breast Cancer//;

      
      if ($time ne "9:59.99")
      {
         print F "$teamName,$category,$time,$raceNo,$distance,$festival,$year\n";     
      }
   }      
}
   
close(F);



